<?php

namespace app\admin\controller;
use app\common\model\Menu as MenuModel;
use app\common\model\AuthGroup as AuthGroupModel;
use app\common\lib\Util;

class AuthGroup extends Base
{
    public function index(AuthGroupModel $authGroup)
    {
        $field=$authGroup->order('id', 'desc')->select();
        $this->assign('authGroup',$field);
        return $this->fetch();
    }


    public function create()
    {
        $menuData = MenuModel::select();
        $this->assign('menuData',$menuData);
        return $this->fetch();
    }


    public function add(AuthGroupModel $agm)
    {
        if(request()->isPost()){
           $rst=$agm->add(input('post.'));
           if($rst['valid']){
               return Util::apiResponse([],$rst['msg'],1);
           }else{
               return Util::apiResponse([],$rst['msg'],-1);
           }
        }
        return $this->view->fetch();
    }


    public function edit(AuthGroupModel $agm){
        $id = input('param.id');
        $auth = $agm->where('id', $id)->find();
        $auth['menu_auth'] = explode(',', $auth['menu_auth']);
        $this->assign("auth", $auth);
        $menuData = MenuModel::select();
        $this->assign('menuData',$menuData);
        return $this->view->fetch();
    }


    public function update(AuthGroupModel $agm)
    {
            $rst = $agm->edit(input('put.'));
            if ($rst['valid']) {
                return Util::apiResponse([], $rst['msg'], 1);
            } else {
                return Util::apiResponse([], $rst['msg'], -1);
            }
            return $this->view->fetch();
    }


    public function delete()
    {
        $id=input("param.id");
        AuthGroupModel::where('id',$id)->delete();
        return Util::apiResponse([],'角色删除成功',1);
    }


    public function status(AuthGroupModel $agm1)
    {
     $status_data = $agm1->status_edit(input('post.'));
     if ($status_data['valid']){
         return Util::apiResponse([],$status_data['msg'], 1);
     }else{
         return Util::apiResponse([],$status_data['msg'], -1);
     }
    }
}
